package com.starsoft.trade.trader;

import java.io.InputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.frame.fileimport.ExcelReader;
import com.starsoft.frame.fileimport.FileReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.trade.bean.TradeAcount;
import com.starsoft.trade.bean.TradeAcountStatus;
import com.starsoft.trade.bean.TradeBill;
import com.starsoft.trade.order.Order;

public class HtTrader extends AbstractTrader {

	private static Logger logger = LoggerFactory.getLogger(HtTrader.class);

	public void init(TradeAcount acount, Map<String, String> apiMap, TraderContextService service) {
		super.init(acount, apiMap, service);
	}

	@Override
	public void login() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public TradeAcountStatus getPortflio() {
		throw new RuntimeException("Not Support");
	}

	@Override
	public List<TradeBill> getHistory(Date startDate) {
		throw new RuntimeException("Not Support");
	}

	@Override
	public void trade(List<Order> order) {
		throw new RuntimeException("Not Support");
	}

	@Override
	public List<TradeBill> getEntrust() {
		throw new RuntimeException("Not Support");
	}

	@Override
	public void cancelEntrust(List<TradeBill> bills) {
		throw new RuntimeException("Not Support");
	}

	@Override
	public List<TradeBill> getHistory(InputStream is) {
		List<Column> columnList = new ArrayList<Column>();
		FileReader reader=new ExcelReader(ExcelReader.EXCEL2010, false);
		columnList.add(new Column("成交日期", "billDate", ValueType.Integer, NullType.notnull));
		columnList.add(new Column("证券代码", "ticker", ValueType.String, NullType.notnull));
		columnList.add(new Column("证券名称", "secName", ValueType.String, NullType.notnull));
		columnList.add(new Column("合同编号", "billSerialnumber", ValueType.Integer, NullType.notnull));
		columnList.add(new Column("成交数量", "secNum", ValueType.Double, NullType.notnull));
		columnList.add(new Column("成交均价", "secPrice", ValueType.Double, NullType.notnull));
		columnList.add(new Column("成交金额", "billTradeMoney", ValueType.Double, NullType.notnull));
		columnList.add(new Column("手续费", "feeCommision", ValueType.Double, NullType.nullable));
		columnList.add(new Column("印花税", "feeTax", ValueType.Double, NullType.notnull));
		columnList.add(new Column("其他杂费", "feeOther", ValueType.Double, NullType.notnull));
		columnList.add(new Column("发生金额", "billMoney", ValueType.Double, NullType.notnull));
		columnList.add(new Column("摘要", "summary", ValueType.String, NullType.notnull));
		DataTable ob= reader.read(is, new Table(TitleType.OnlyCode,columnList));
		
		List<TradeBill> billList = new ArrayList<>();
		for (Map<String, Object> mapObject : ob.getData()) {
			try {
				String ticker = ((String) mapObject.get("ticker")).split("\\.")[0];
				while(ticker.length()<6){
					ticker = "0" + ticker;
				}
				SmdcSecurity security = contextService.getSecurityByTicker(ticker);
				if (security == null) {
					continue;
				}
				Integer serialnumber = (Integer) mapObject.get("billSerialnumber");
				Integer billDate = (Integer) mapObject.get("billDate");
				Timestamp billTime = new Timestamp(DateUtil.toDate(billDate+"", "yyyyMMdd").getTime());
				TradeBill bill = contextService.getBillService().getBillBySerialnumber(acount.getAcountId(), serialnumber);
				if(bill == null){
					bill = new TradeBill();
					bill.setAcountId(acount.getAcountId());
				}
				MapUtil.updateBean(bill, mapObject);
				bill.setBillTime(billTime);
				bill.setSecId(security.getSecId());
				bill.setBillDesc("导入");
				if(bill.getSecNum()!=0){
					bill.setBillTradeMoney(-bill.getSecNum()*bill.getSecPrice());
				}
				contextService.getBillService().dealBillType(bill);
				bill.setFeeAll(bill.getFeeCommision()+bill.getFeeTax()+bill.getFeeOther());
				billList.add(bill);
			} catch (Exception e) {
				logger.error("map to bean error " + mapObject.get("secId"), e);
			}
		}
		return billList;
	}
	
}
